package cn.genmer.test.security.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.address_purification.model.v20191118.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 阿里AI-地址标准化
 *
 * 在线测试地址： https://addrp.console.aliyun.com/onlinetesting
 * API参考文档： https://help.aliyun.com/document_detail/169625.html
 */
public class AliyunAddressUtils {

    private Logger log = LoggerFactory.getLogger(AliyunAddressUtils.class);

//    public static final String ACCESS_KEY_ID = "LTAI5t7m5bSzys1YRkojYK6p";
    public static final String ACCESS_KEY_ID = "LTAI5tFsZoSLB7JokoRXuDrP";
//    public static final String ACCESS_KEY_SECRET = "ny9s1C2KflrXezEGZczaA65IONLkIS";
    public static final String ACCESS_KEY_SECRET = "gBIStTbgmOF2dRYAmpkUaEXbD7UIk5";
    public static final String PROJECT_APPKEY = "Xctpyqxqes7f";

    public static final String API_TYPE_COMPLETEADDRESS = "complete";
    public static final String API_TYPE_STRUCTUREADDRESS = "structure";
    public static final String API_TYPE_CORRECTADDRESS = "address_correct";

    public static void main(String[] args) throws ClientException {
        // 构建公共参数
//        String address = "浙江省中通快运总部";
//        System.out.println(Runtime.getRuntime().availableProcessors());

//        System.out.println("地址补全");
//        System.out.println(completeAddress(address));
//
//        System.out.println("地址结构化");
//        System.out.println(structureAddress(address));
//
//        System.out.println("地址纠错");
//        System.out.println(correctAddress(address));

        // 去除字符串所有空格
        String a = "  aaaaa  a  ";
        System.out.println(a.trim());
        System.out.println(a.replaceAll("\\s*", ""));
    }

    /**
     * 阿里云AI - 地址补全
     * @param address
     * @return
     */
    public static synchronized String completeAddress(String address) {
        // 构建公共参数
        IAcsClient client = getAcsClient();

        // 构建请求 - 地址补全
        CompleteAddressRequest request = new CompleteAddressRequest();
        request.setServiceCode("addrp");
        request.setAppKey(PROJECT_APPKEY);
        request.setText(address);
//        request.setDefaultProvince("浙江省");
//        request.setDefaultCity("杭州市");
//        request.setDefaultDistrict("余杭区");
        CompleteAddressResponse response = null;
        try {
           response = client.getAcsResponse(request);
        } catch (ClientException e){
//            log.error(e.toString());
            e.printStackTrace();
            System.out.println("报错的地址为：");
            System.out.println(address);
        }
        if (response == null){
            return "获取结果失败，请查看控制台或者日志！";
        }
        return response.getData().toString();
    }

    /**
     * 阿里云AI - 地址结构化
     * @param address
     * @return
     */
    public static String structureAddress(String address) {
        // 构建公共参数
        IAcsClient client = getAcsClient();

        // 构建请求 - 地址补全
        StructureAddressRequest request = new StructureAddressRequest();
        request.setServiceCode("addrp");
        request.setAppKey(PROJECT_APPKEY);
        request.setText(address);
        StructureAddressResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e){
//            log.error(e.toString());
            e.printStackTrace();
            System.out.println("报错的地址为：");
            System.out.println(address);
        }
        if (response == null){
            return "获取结果失败，请查看控制台或者日志！";
        }
        return response.getData().toString();
    }

    /**
     * 阿里云AI - 地址纠错
     * @param address
     * @return
     */
    public static String correctAddress(String address) {
        // 构建公共参数
        IAcsClient client = getAcsClient();

        // 构建请求 - 地址补全
        CorrectAddressRequest request = new CorrectAddressRequest();
        request.setServiceCode("addrp");
        request.setAppKey(PROJECT_APPKEY);
        request.setText(address);
        CorrectAddressResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e){
//            log.error(e.toString());
            e.printStackTrace();
        }
        if (response == null){
            return "获取结果失败，请查看控制台或者日志！";
        }
        return response.getData().toString();
    }

    /**
     * 阿里云AI - 地理编码
     * @param address
     * @return
     */
    public static String geocoding(String address) {
        // 构建公共参数
        IAcsClient client = getAcsClient();

        // 构建请求 - 地理编码
        GetAddressGeocodeRequest request = new GetAddressGeocodeRequest();
        request.setServiceCode("addrp");
        request.setAppKey(PROJECT_APPKEY);
        request.setText(address);
        GetAddressGeocodeResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e){
//            log.error(e.toString());
            e.printStackTrace();
        }
        if (response == null){
            return "获取结果失败，请查看控制台或者日志！";
        }
        return response.getData().toString();
    }

    /**
     *  构建公共参数
     * @return
     */
    private static IAcsClient getAcsClient(){
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                ACCESS_KEY_ID,
                ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

}